package xufei.order.service.impl;

import com.xufei.common.domain.order.Order;
import com.xufei.common.domain.storage.Storage;
import com.xufei.common.domain.user.User;
import com.xufei.storage.feign.StorageFeignClient;
import com.xufei.user.feign.UserFeignClient;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xufei.order.mapper.OrderMapper;
import xufei.order.service.OrderService;
import java.math.BigDecimal;
import java.util.List;


@Service
public class OrderServiceImpl implements OrderService {

    private final OrderMapper orderMapper;

    private final UserFeignClient userFeignClient;

    private final StorageFeignClient storageFeignClient;

    public OrderServiceImpl(OrderMapper orderMapper, UserFeignClient userFeignClient, StorageFeignClient storageFeignClient) {
        this.orderMapper = orderMapper;
        this.userFeignClient = userFeignClient;
        this.storageFeignClient = storageFeignClient;
    }

    @Override
    @GlobalTransactional(rollbackFor = Exception.class)
    @Transactional(rollbackFor = Exception.class)
    public int createOrder(Order order) {
        int count = orderMapper.createOrder(order);
        //分支事务
        Storage storage = new Storage();
        storage.setProductId(234);
        storage.setResidue(new BigDecimal(900));
       storageFeignClient.updateResidue(storage);

        //分支事务
        User user = new User();
        user.setUserId(1);
        user.setIntegral(new BigDecimal(100));
        userFeignClient.updateIntegral(user);
        return count;
    }

    @Override
    public List<Order> selectOrderList(Order order) {
        return orderMapper.selectOrderList(order);
    }
}
